package com.sangmo.fts.api.storage;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.storage.interfaces.CsfLoanAuditRequest;
import com.sangmo.fts.process.storage.interfaces.CsfLoanCreation;
import com.sangmo.fts.process.storage.interfaces.CsfLoanDepositResponse;
import com.sangmo.fts.process.storage.model.CsfLoan;
import com.sangmo.fts.process.storage.service.CsfLoanService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/csfLoan/v1")
@Access("csfLoan")
public class CsfLoanApi {

    @Autowired
    private CsfLoanService csfLoanService;

    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_LOAN_BUYER_APPLY_7)
    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<CsfLoan> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid CsfLoanCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(csfLoanService.create(obj));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<CsfLoan>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfLoan.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfLoanService.search(ps));
    }

    /** 受理 **/
    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_LOAN_BANK_ACCEPT_8)
    @Access(":accept")
    @PostMapping(value = "/{trainingId}/{id}/accept")
    public @ResponseBody
    ApiResponse accept(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        csfLoanService.accept(id);
        return ApiResponse.make();
    }

    @Access(":pay")
    @PostMapping(value = "/{trainingId}/{id}/deposit/calc")
    public @ResponseBody
    ApiResponse<CsfLoanDepositResponse> calc(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        return ApiResponse.make(csfLoanService.calcDeposit(id));
    }


    /** 缴纳保证金 **/
    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_LOAN_BUYER_DEPOSIT_9)
    @Access(":pay")
    @PostMapping(value = "/{trainingId}/{id}/deposit/pay")
    public @ResponseBody
    ApiResponse pay(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        csfLoanService.pay(id);
        return ApiResponse.make();
    }

    /** 审核 **/
    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_LOAN_BANK_AUDIT_12)
    @Access(":audit")
    @PostMapping(value = "/{trainingId}/{id}/audit")
    public @ResponseBody
    ApiResponse audit(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid CsfLoanAuditRequest request) {
        csfLoanService.audit(id, request);
        return ApiResponse.make();
    }

    @Access(":assuranceSelection")
    @PostMapping({"/{trainingId}/assurance/selection"})
    @ResponseBody
    public ApiResponse<PageResult<CsfLoan>> assuranceSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfLoan.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfLoanService.assuranceSelection(ps));
    }

    @Access(":draftSelection")
    @PostMapping({"/{trainingId}/draft/selection"})
    @ResponseBody
    public ApiResponse<PageResult<CsfLoan>> draftSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfLoan.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfLoanService.draftSelection(ps));
    }

    @Access(":repaymentSelection")
    @PostMapping({"/{trainingId}/repayment/selection"})
    @ResponseBody
    public ApiResponse<PageResult<CsfLoan>> repaymentSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfLoan.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfLoanService.repaymentSelection(ps));
    }

    @Access(":ladingBillSelection")
    @PostMapping({"/{trainingId}/ladingBill/selection"})
    @ResponseBody
    public ApiResponse<PageResult<CsfLoan>> ladingBillSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfLoan.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfLoanService.ladingBillSelection(ps));
    }
}
